package com.xwx.common.config;

import org.apache.log4j.Logger;

import com.jfinal.config.Plugins;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.xwx.common.model.DataDicModel;
import com.xwx.common.model.DepExtModel;
import com.xwx.common.model.DepModel;
import com.xwx.common.model.ErrorLogModel;
import com.xwx.common.model.FunModel;
import com.xwx.common.model.LoginLogModel;
import com.xwx.common.model.OperLogModel;
import com.xwx.common.model.RoleAccessModel;
import com.xwx.common.model.RoleModel;
import com.xwx.common.model.UserDepModel;
import com.xwx.common.model.UserExtModel;
import com.xwx.common.model.UserModel;
import com.xwx.common.model.UserRoleModel;
/**
* @Description:核心配置
* @author:Dream QQ:68697015
* @time:2015年8月31日 上午11:27:41
 */
public class AppConfigDB {
	protected static Logger log=Logger.getLogger(AppConfigDB.class);
	
	/**
	 * 资源
	 */
	public static final String FUN="gbdc_fun";
	/**
	 * 单位
	 */
	public static final String DEP="gbdc_dep";
	
	/**
	 * 单位扩展
	 */
	public static final String DEP_EXT="gbdc_dep_ext";
	
	/**
	 * 用户
	 */
	public static final String USER="gbdc_user";
	/**
	 * 用户扩展
	 */
	public static final String USER_EXT="gbdc_user_ext";
	
	/**
	 * 用户单位关联
	 */
	public static final String USER_DEP="gbdc_user_dep";
	/**
	 * 角色
	 */
	public static final String ROLE="gbdc_role";
	/**
	 * 用户角色关联
	 */
	public static final String USER_ROLE="gbdc_user_role";
	/**
	 * 角色权限
	 */
	public static final String ROLE_ACCESS="gbdc_role_access";
	/**
	 * 数据字典
	 */
	public static final String DATA_DIC="gbdc_data_dic";
	
	//日志
	public static final String LOGIN_LOG="gbdc_login_log";//登录日志
	public static final String OPER_LOG="gbdc_oper_log";//操作日志
	public static final String ERROR_LOG="gbdc_eror_log";//错误日志
	
	static{
		PropKit.use("DbConfig.txt");//加载数据库配置
	}
	
	/**
	 * 核心数据库连接配置
	 * @param me
	 */
	public static void CoreDBConfig(Plugins me){
			DruidPlugin druidPlugin = new DruidPlugin(getDbValue("dev_url"), getDbValue("dev_user"), getDbValue("dev_password"),getDbValue("dev_driverClass"));
			ActiveRecordPlugin arp = new ActiveRecordPlugin("dev",druidPlugin);
			if(getDbValue("dev_dialect").equals("sqlserver"))arp.setDialect(new AnsiSqlDialect());
			arp.setDevMode(PropKit.use("DbConfig.txt").getBoolean("dev_devMode",true));
			arp.setShowSql(PropKit.use("DbConfig.txt").getBoolean("dev_showSql",true));	
			//映射核心数据库表
			arp.addMapping(FUN, FunModel.class);
			arp.addMapping(USER, UserModel.class);
			arp.addMapping(USER_EXT, UserExtModel.class);
			arp.addMapping(USER_DEP, UserDepModel.class);
			arp.addMapping(DEP, DepModel.class);
			arp.addMapping(DEP_EXT, DepExtModel.class);
			arp.addMapping(ROLE, RoleModel.class);
			arp.addMapping(USER_ROLE, UserRoleModel.class);
			arp.addMapping(ROLE_ACCESS, RoleAccessModel.class);
			//日志
			arp.addMapping(LOGIN_LOG, LoginLogModel.class);
			arp.addMapping(OPER_LOG, OperLogModel.class);
			arp.addMapping(ERROR_LOG, ErrorLogModel.class);
			//数据字典
			arp.addMapping(DATA_DIC, DataDicModel.class);
			me.add(druidPlugin).add(arp);		
			log.info("核心数据库加载成功");
	}
	/**
	 * WEB应用数据库配置
	 * @return ActiveRecordPlugin 可用于其它插件使用
	 */
	public static ActiveRecordPlugin WEBDBCofin(Plugins me){
		DruidPlugin druidPlugin = new DruidPlugin(getDbValue("web_url"), getDbValue("web_user"), getDbValue("web_password").trim(),getDbValue("web_driverClass"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin("WEB",druidPlugin);
		if(getDbValue("web_dialect").equals("sqlserver"))arp.setDialect(new AnsiSqlDialect());
		arp.setDevMode(PropKit.use("DbConfig.txt").getBoolean("web_devMode",true));
		arp.setShowSql(PropKit.use("DbConfig.txt").getBoolean("web_showSql",true));
		me.add(druidPlugin);
		log.info("WEB应用数据库加载成功");
		return arp;
	}
	/**
	 * 加载数据库配置文件,jfinal中多个配置文件，第一个为默认，后续的需增加use
	 * @param key 要获取的KEY
	 * @return 返回的value
	 */
	private static String getDbValue(String key){
		return PropKit.use("DbConfig.txt").get(key);
	}
}
